﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Starfire3d.Interfaces;

namespace Starfire3d.Data.Objects
{
    public class ShipSystem : IDataItem, ICloneable
    {
        #region Attributes
        /*
  [Name:Sprint Missile]
  [Code:Ga]
  [SL:0]
  [Size:3]
  [Cost:22]
         */
        private IDataContainer gameData = null;

        private string name;
        private string code;
        private int sl;
        private double size;
        private double cost;
        private int modifier;

        private bool isDestroyed = false;
        #endregion

        #region Constructor
        public ShipSystem()
        {
            gameData = new DataContainer();
        }
        #endregion

        #region Properties
        public IDataContainer Data
        {
            get { return gameData; }
        }
        public string Name
        {
            get { return name; }
        }
        public string Code
        {
            get { return code; }
        }
        public int SL
        {
            get { return sl; }
        }
        public double Size
        {
            get { return size; }
        }
        public double Cost
        {
            get { return cost; }
        }
        public int Modifier
        {
            get { return modifier; }
        }
        public bool IsEngine
        {
            get { return Data.GetBool("engine"); }
        }
        public bool IsSensor
        {
            get { return Data.GetBool("sensor"); }
        }
        public bool IsLifeSupport
        {
            get { return Data.GetBool("lifeSupport"); }
        }
        public bool IsWeapon
        {
            get { return Data.GetBool("weapon"); }
        }
        public bool IsPointDefence
        {
            get { return Data.GetBool("pointdefence"); }
        }
        public bool IsDestroyed
        {
            get { return isDestroyed; }
            set { isDestroyed = value; }
        }
        #endregion

        #region Public Methods
        public bool BuildFromMetaData(MetaObject meta)
        {
            //base.BuildFromMetaData(meta);

            foreach (string token in meta.TokenList)
            {
                string[] tokens = token.Split(':');

                // Single line Tags
                if (tokens.Count() == 1)
                {
                    Data.Add(tokens[0].ToUpper(), true, true);
                    //switch (tokens[0].ToLower())
                    //{
                    //    case "engine":
                    //        isEngine = true;
                    //        break;
                    //    case "sensor":
                    //        isSensor = true;
                    //        break;
                    //    case "lifeSupport":
                    //        isLifeSupport = true;
                    //        break;
                    //    case "weapon":
                    //        isWeapon = true;
                    //        break;
                    //    case "pointdefence":
                    //        isPointDefence = true;
                    //        break;
                    //}
                    continue;
                }

                if (tokens[0].ToLower() == "name")
                {
                    name = tokens[1];
                    continue;
                }
                if (tokens[0].ToLower() == "code")
                {
                    code = tokens[1];
                    continue;
                }
                if (tokens[0].ToLower() == "sl")
                {
                    sl = int.Parse(tokens[1]);
                    continue;
                }
                if (tokens[0].ToLower() == "size")
                {
                    size = double.Parse(tokens[1]);
                    continue;
                }
                if (tokens[0].ToLower() == "cost")
                {
                    cost = double.Parse(tokens[1]);
                    continue;
                }
                if (tokens[0].ToLower() == "modifier")
                {
                    modifier = int.Parse(tokens[1]);
                    continue;
                }
            }

            return true;
        }

        public override string ToString()
        {
            if (!isDestroyed)
                return code;
            else
                return "x";
        }
        #endregion

        #region ICloneable Members
        public object Clone()
        {
            return this.MemberwiseClone();
        }
        #endregion
    }
}
